//
//  CZMainViewController.swift
//  czbkiosweibo5
//
//  Created by zhangping on 15/10/12.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        let mainTabBar = CZMainTabBar()
//        mainTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
//        setValue(mainTabBar, forKey: "tabBar")
        
        // 设置tabbar的外观颜色
        tabBar.tintColor = UIColor.orangeColor()

        // 添加子控件
        addChildViewControllers()
        
        // 不能再这里设置撰写按钮,这时候tabBar的子视图还没有创建好,撰写按钮会被UITabBarButton挡住
//        print("tabbar: \(tabBar.subviews)")
//        let width = tabBar.bounds.width / 5
//        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: tabBar.bounds.height)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 设置撰写按钮位置
        let width = tabBar.bounds.width / 5
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: tabBar.bounds.height)
    }
    
    // 不要将点击事件方法 添加private
    func composeClick() {
        // 判断用户是否登录,只有用户登录后在能点击跳到发微博界面,否则跳到登录界面
        
        // 创建发微博控制器
        let controller = CZComposeViewController()
        
        // modal出一个控制器,包装导航控制器
        presentViewController(UINavigationController(rootViewController: controller), animated: true, completion: nil)
    }
    
    // MARK: - 私有方法
    /**
    添加所有子控件
    */
    private func addChildViewControllers() {
        let homeVC = CZHomeTableViewController()

        // 设置tabBarItem的title颜色
//        homeVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: UIControlState.Normal)
        // ?表示.如果前面有值就执行后面的代码,前面没有值就不执行后面的代码
        //        homeVC.tabBarItem.selectedImage = UIImage(named: "tabbar_home_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        let messageVC = CZMessageTableViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 添加一个控制器,用来在tabBar上面占位置
        // 如果图片名称为"" 会报: CUICatalog: Invalid asset name supplied:
        addChildViewController(UIViewController(), title: "", imageName: "aa")
        
        let discoverVC = CZDiscoverTableViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        let profileVC = CZProfileTableViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(controller: UIViewController, title: String, imageName: String) {
        // 设置controller的title.会影响 tabBarItem.title, navigationItem.title
        controller.title = title
        
        // 设置tabbar的图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
        // 包装导航控制器,添加到子控制器
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    // MARK: - 懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        // 按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.tabBar.addSubview(button)
        return button
    }()
}
